package org.onvivals.module.pms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.onvivals.module.pms.entity.PmsCode;
import org.onvivals.module.pms.mapper.PmsCodeMapper;
import org.onvivals.module.pms.service.IPmsCodeService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PmsCodeServiceImpl extends ServiceImpl<PmsCodeMapper,PmsCode> implements IPmsCodeService {


	@Override
	public boolean checkCode(String code, Long userId) {
		PmsCode pmsCode = this.getOne(Wrappers.<PmsCode>lambdaQuery().eq(PmsCode::getCode, code));
		if (pmsCode==null){
			return false;
		}
		if (ObjectUtil.isNotEmpty(pmsCode.getUserId())){
			return pmsCode.getUserId().equals(userId);
		}else {
			pmsCode.setUserId(userId);
			pmsCode.setActiveTime(new Date());
			pmsCode.setStatus(1);
			this.updateById(pmsCode);
		}
		return true;
	}
}
